package com.study.leetcode;

public class Solution_112 {
    public static boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        if (root.right == null && root.left == null) {
            return targetSum - root.val == 0;
        } else if (root.right != null && root.left == null) {
            return hasPathSum(root.right, targetSum - root.val);
        } else if (root.right == null) {
            return hasPathSum(root.left, targetSum - root.val);
        } else {
            return hasPathSum(root.left, targetSum - root.val) ||
                    hasPathSum(root.right, targetSum - root.val);
        }
    }

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(-3);
        TreeNode node = new TreeNode(-2, null, node1);
        System.out.println("hasPathSum = " + hasPathSum(node, -5));
    }
}
